package com.ygqh.baby.controller.admin;

import com.ygqh.baby.ao.ContractStatus;
import com.ygqh.baby.ao.FileTemplate;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.controller.utils.DocumentHandler;
import com.ygqh.baby.po.YgSupplierReconciliation;
import com.ygqh.baby.po.YgSupplierSchedule;
import com.ygqh.baby.service.FileService;
import com.ygqh.baby.service.YgSupplierReconciliationService;
import com.ygqh.baby.service.YgSupplierScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("/admin/supplierschedule")
public class SupplierScheduleController  extends BaseController<Object, java.lang.Long>{
	
    @Autowired
	private YgSupplierScheduleService ygSupplierScheduleService;
    @Autowired
 	private FileService fileService;
    @Autowired
    private YgSupplierReconciliationService ygSupplierReconciliationService;
    
   
	@RequestMapping("searchSchedule")
	@ResponseBody
	public Map<String,Object> searchSchedule(QueryInfo queryInfo) {	
		
		ResultSet<YgSupplierSchedule> resultSet=ygSupplierScheduleService.search(queryInfo,getSupplier().getId());
		
		return resultSet.toDatatables(null);
	}
	@RequestMapping("exportData")
	public void exportData(Date startDate, Date endDate, HttpServletRequest request, HttpServletResponse response) {
		Long supplierId=getSupplier().getId();
		Map<String, Object> dataMap =
				ygSupplierReconciliationService.findExportDataDetail(startDate, endDate, supplierId);
		byte[] bytes = fileService.downloadFromOss("template/yiigoo_supplier_order.xlsx");
		DocumentHandler.DownloadExcel(bytes, "对账数据.xlsx", dataMap, request, response);
	}
	@RequestMapping(value = "searchSupplierReconciliation")
	@ResponseBody
	public Map<String, Object> searchSupplierReconciliation(QueryInfo queryInfo, String q,
															ContractStatus reconciliationStatus) {
		Long supplierId=getSupplier().getId();
		ResultSet<YgSupplierReconciliation> resultSet =
				ygSupplierReconciliationService.search(queryInfo, q, supplierId, reconciliationStatus);
		return resultSet.toDatatables(null);
	}
	@RequestMapping("exportSupplierReconciliation")
	public void exportSupplierReconciliation(@RequestParam Long reconciliationId,
											 HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> dataMap = ygSupplierReconciliationService.findExportData(reconciliationId);
		byte[] bytes = fileService.downloadFromOss(FileTemplate.SupplyReconciliationExport.getCode());
		DocumentHandler.DownloadExcel(bytes, FileTemplate.SupplyReconciliationExport.getTitle(),
				dataMap, request, response);
	}
}
